package com.hnevc.wangyu.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.hnevc.wangyu.R;
import com.hnevc.wangyu.bean.UserBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView title_back;
    private TextView title_text;
    private EditText regist_et_name;
    private EditText regist_et_toname;
    private EditText regist_et_emile;
    private EditText regist_et_plone;
    private Spinner regist_sp_college;
    private Spinner regist_sp_profession;
    private EditText regist_et_password;
    private EditText regist_et_aspassword;
    private Button regist_btn_regist;
    private String name;
    private String toname;
    private String emile;
    private String plone;
    private String password;
    private String aspassword;
    private String department;
    private String major;
    private UserBean userBean;
    private static final String TAG = "RegistActivity";
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initEvent();

    }

    private void initEvent() {

        regist_sp_college.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] languages = getResources().getStringArray(R.array.Data);
                Toast.makeText(RegistActivity.this, "你点击的是:"+languages[position], Toast.LENGTH_LONG).show();
                department = languages[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        regist_sp_profession.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] languages = getResources().getStringArray(R.array.Data2);
                Toast.makeText(RegistActivity.this, "你点击的是:"+languages[position], Toast.LENGTH_LONG).show();
                major = languages[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initView() {
        title_back = (ImageView) findViewById(R.id.title_back);
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("用户注册");
        regist_et_name = (EditText) findViewById(R.id.regist_et_name);
        regist_et_toname = (EditText) findViewById(R.id.regist_et_toname);
        regist_et_emile = (EditText) findViewById(R.id.regist_et_emile);
        regist_et_plone = (EditText) findViewById(R.id.regist_et_plone);
        regist_sp_college = (Spinner) findViewById(R.id.regist_sp_college);
        regist_sp_profession = (Spinner) findViewById(R.id.regist_sp_profession);
        regist_et_password = (EditText) findViewById(R.id.regist_et_password);
        regist_et_aspassword = (EditText) findViewById(R.id.regist_et_aspassword);
        regist_btn_regist = (Button) findViewById(R.id.regist_btn_regist);

        title_back.setOnClickListener(this);
        regist_btn_regist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.regist_btn_regist:
                if (submit()==true){
                    getRegist();
                }

                break;
        }
    }

    private void getRegist() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                final String url = "http://anli-wechat.boruiman.com/freshmenapp/user/regist";
                OkHttpClient client = new OkHttpClient();
                RequestBody formbody = new FormBody.Builder()
                        .add("name",name)
                        .add("password",password)
                        .add("email",emile)
                        .add("telephone",plone)
                        .add("department",department)
                        .add("major",major)
                        .add("role","学生")
                        .build();
                 Request request = new Request.Builder()
                        .url(url)
                        .post(formbody)
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e.getMessage() );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        if (!response.isSuccessful()){
                            Log.e(TAG, "onResponse: 请求失败 "+ response.message() );
                        }
                        Log.e(TAG, "onResponse: "+response );
                        Gson gson = new Gson();

                    userBean = gson.fromJson(response.body().string(), UserBean.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (userBean.getMsg().equals("成功")){
                                Toast.makeText(RegistActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            Toast.makeText(RegistActivity.this, "注册"+userBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    });

                    }
                });

//                try {
//                    JSONObject responseByPOST = OkHttpUtil.getOkHttpUtil().getResponseByPOST("http://anli-wechat.boruiman.com/freshmenapp/user/regist",
//                            "name="+ name+
//                            "&password="+password+
//                                    "&email="+emile+
//                                    "&telephone="+plone+
//                                    "&department="+department+
//                                    "&major="+major+
//                                    "&role="+"学生"
//                    );
//                    Gson gson = new Gson();
//                    userBean = gson.fromJson(responseByPOST.toString(), UserBean.class);
//
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//
//                            if (userBean.getMsg().equals("成功")){
//                                Toast.makeText(RegistActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
//                                finish();
//                            }
//                            Toast.makeText(RegistActivity.this, "注册"+userBean.getMsg(), Toast.LENGTH_SHORT).show();
//                        }
//                    });
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
                finish();

            }
        }).start();

    }

    private boolean submit() {
        // validate
        name = regist_et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return false;
        }

        toname = regist_et_toname.getText().toString().trim();
        if (TextUtils.isEmpty(toname)) {
            Toast.makeText(this, "真实姓名", Toast.LENGTH_SHORT).show();
            return false;
        }

        emile = regist_et_emile.getText().toString().trim();
        if (TextUtils.isEmpty(emile)) {
            Toast.makeText(this, "邮箱：安全邮箱用来找回密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        plone = regist_et_plone.getText().toString().trim();
        if (TextUtils.isEmpty(plone)) {
            Toast.makeText(this, "电话", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = regist_et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        aspassword = regist_et_aspassword.getText().toString().trim();
        if (TextUtils.isEmpty(aspassword)) {
            Toast.makeText(this, "重复密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!password.equals(aspassword)){
            Toast.makeText(this, "重复密码错误！", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;


    }
}